package com.psbc.redis_study.controller;

import com.psbc.redis_study.model.VideoDo;
import com.psbc.redis_study.utils.JsonData;
import com.psbc.redis_study.vo.UserPointVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/v1/rank")
public class RankController {
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String DAILY_RANK_KEY = "video:rank:daily";

    /**
     * 这个是非实时统计数据，每日的热销榜单
     * 其实这个榜单基本都是后台统计数据传入以及后期人工运营的结果
     * @return
     */
    @GetMapping("/daily_rank")
    public JsonData dailyRank(){
        List<VideoDo> list = redisTemplate.opsForList().range(DAILY_RANK_KEY, 0, -1);
        return JsonData.buildSuccess(list);
    }

    /**
     * 返回全部榜单，从大到小
     */
    @RequestMapping("/real_rank1")
    public JsonData realRank1(){
        BoundZSetOperations operations = redisTemplate.boundZSetOps("point:rank:real");
        Set realRank = operations.reverseRange(0, -1);
        return JsonData.buildSuccess(realRank);
    }
    /**
     * 返回全部榜单，从小到大
     */
    @RequestMapping("/real_rank2")
    public JsonData realRank2(){
        BoundZSetOperations operations = redisTemplate.boundZSetOps("point:rank:real");
        Set realRank = operations.range(0, -1);
        return JsonData.buildSuccess(realRank);
    }

    /**
     * 返回全部榜单，从大到小,指定返回的长度
     */
    @RequestMapping("/real_rank3")
    public JsonData realRank3(){
        BoundZSetOperations operations = redisTemplate.boundZSetOps("point:rank:real");
        Set realRank = operations.reverseRange(0, 3);
        return JsonData.buildSuccess(realRank);
    }
    /**
     * 查看某个用户的排名
     */
    @RequestMapping("/my_rank")
    public JsonData myRank(String name,String phone){
        BoundZSetOperations operations = redisTemplate.boundZSetOps("point:rank:real");
        UserPointVo userPointVo = new UserPointVo(name, phone);
        Long rank = operations.reverseRank(userPointVo);
        return JsonData.buildSuccess(++rank);
    }
    /**
     * 通过给某个用户加积分，来更新某个用户的排名
     */
    @RequestMapping("/up_rank")
    public JsonData upRank(String name,String phone,int point){
        BoundZSetOperations operations = redisTemplate.boundZSetOps("point:rank:real");
        UserPointVo userPointVo = new UserPointVo(name, phone);
        Double rank = operations.incrementScore(userPointVo, point);
        Set range = operations.range(0, -1);
        return JsonData.buildSuccess(range);
    }
    /**
     * 查看个人积分
     */
    @RequestMapping("/my_point")
    public JsonData myPoint(String name,String phone){
        BoundZSetOperations operations = redisTemplate.boundZSetOps("point:rank:real");
        UserPointVo userPointVo = new UserPointVo(name, phone);
        Double score = operations.score(userPointVo);
        return JsonData.buildSuccess(score);
    }
}
